package com.jlnk.androidfmc.fragment.fragments.homefragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.adapter.ProductAdapter;
import com.jlnk.androidfmc.database.ProductDatabaseHelper;
import com.jlnk.androidfmc.entity.Product;
import com.jlnk.androidfmc.presenter.CategoryPresenter;
import com.jlnk.androidfmc.view.CategoryView;

import java.util.ArrayList;
import java.util.List;

public class GrainsOilFragment extends Fragment implements CategoryView {
    private ProductAdapter adapter;
    private CategoryPresenter presenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_grains_oil, container, false);

        // 使用 GridLayoutManager 来以两列的形式显示项目
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);

        // 初始化 adapter
        List<Product> productList = new ArrayList<>();
        adapter = new ProductAdapter(productList);
        recyclerView.setAdapter(adapter);

        // 初始化 presenter 并加载特定分类的商品
        presenter = new CategoryPresenter((CategoryView) this, getContext());
        presenter.loadProductsByCategory(ProductDatabaseHelper.Category.GRAINS_OIL);

        return view;
    }

    @Override
    public void showProducts(List<Product> productList) {
        adapter.setProductList(productList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(String errorMessage) {

    }
}